const express = require('express')
const expressjwt = require('express-jwt')
const home = require('./router/home')
const cate = require('./router/cate')
const user = require('./router/user')
const admin = require('./router/admin')
const config = require('./config/config')
const app = express()
const cors = require('cors')

// 解析JSON格式的请求体数据
app.use(express.json())
// 解析URL-encoded格式的请求体数据
app.use(express.urlencoded({extended: false}))

app.use(function(req,res,next) {
    res.cc = function(err,status = 501) {
        res.send({
            status,
            message: err instanceof Error ? err.message : err
        })
    }
    next()
})
app.use(cors())
app.use('/static',express.static('public'))

// 验证token
// app.use(expressjwt.expressjwt({secret:config.secretKey,algorithms:['HS256']}).unless({path:[/^\/user\//]}))

// 小程序用户接口
app.use('/user',user)
app.use('/home',home)
app.use('/cate',cate)

// 后台管理接口
app.use('/admin',admin)
app.listen(10004,()=> {
    console.log("server running at http://127.0.0.1:10004");
})